home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Communications / Networks / MailCheck 0.9x / cdev src / cdev runner.c next >
C/C++ Source or Header  |  1991-06-22  |  4KB  |  211 lines

  1.  
  2. /*
  3.  *  cdev runner.c
  4.  *
  5.  *  Copyright (c) 1991 Symantec Corporation.  All rights reserved.
  6.  *
  7.  */
  8.  
  9. static char CDEV[] = "\p*sample*";
  10.  
  11. static MenuHandle appleMenu;        /*  Apple menu  */
  12.  
  13. #define hiword(x)        (((short *) &(x))[0])
  14. #define loword(x)        (((short *) &(x))[1])
  15.  
  16. extern short Keys : 0x17A;
  17.  
  18. static __A5(void);
  19. static doEvent(EventRecord *event);
  20. static doMenu(long choice);
  21.  
  22. main()
  23. {
  24.     short i, refnum;
  25.     Handle r;
  26.     extern pascal long myMain();
  27.     MenuHandle menu;
  28.     EventRecord event;
  29.     SysEnvRec theWorld;
  30.     register struct jump { short jump; long addr; } *p;
  31.  
  32.         /*  initialize  */
  33.         
  34.     MaxApplZone();
  35.     for (i = 0; i < 10; i++)
  36.         MoreMasters();
  37.     InitGraf(&thePort);
  38.     InitCursor();
  39.     InitFonts();
  40.     InitWindows();
  41.     TEInit();
  42.     InitDialogs(0);
  43.     InitMenus();
  44.     FlushEvents(everyEvent, 0);
  45.         
  46.         /*  look up cdev  */
  47.     
  48.     if (SysEnvirons(1, &theWorld))
  49.         return;
  50.     if (SetVol(0, theWorld.sysVRefNum))
  51.         return;
  52.     if ((refnum = OpenResFile(CDEV)) < 0)
  53.         return;
  54.     if ((r = Get1Resource('cdev', -4064)) == 0)
  55.         return;
  56.     p = (struct jump *) (*r + 16);
  57.     p->jump = 0x4EF9;
  58.     p->addr = (long) myMain;
  59.     ChangedResource(r);
  60.     CloseResFile(refnum);
  61.     
  62.         /*  remember CurrentA5  */
  63.  
  64.     __A5();
  65.     asm {
  66.         move.l    a5,(a0)
  67.     }
  68.  
  69.         /*  build apple menu  */
  70.  
  71.     InsertMenu(appleMenu = NewMenu(1, "\p\024"), 0);
  72.     AddResMenu(appleMenu, 'DRVR');
  73.     
  74.         /*  build remaining menus  */
  75.         
  76.     InsertMenu(menu = NewMenu(2, "\pFile"), 0);
  77.     AppendMenu(menu, "\pOpen/O;Close;Quit/Q");
  78.     InsertMenu(menu = NewMenu(3, "\pEdit"), 0);
  79.     AppendMenu(menu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  80.     DrawMenuBar();
  81.  
  82.         /*  process events  */
  83.     
  84.     for (;;) {
  85.         HiliteMenu(0);
  86.         SystemTask();
  87.         SEvtEnb = false;
  88.         if (GetNextEvent(everyEvent, &event)) {
  89.             if (!SystemEvent(&event))
  90.                 doEvent(&event);
  91.         }
  92.         else if (event.what == nullEvent) {
  93.             if (FrontWindow() == 0)
  94.                 InitCursor();
  95.         }
  96.     }
  97. }
  98.  
  99.  
  100. /*
  101.  *  doEvent - handle event
  102.  *
  103.  */
  104.  
  105. static
  106. doEvent(EventRecord *event)
  107. {
  108.     short part;
  109.     WindowPeek wp;
  110.     static Point where = { 0x0064, 0x0064 };
  111.     
  112.     switch (event->what) {
  113.         case diskEvt:
  114.             if (hiword(event->message)) {
  115.                 InitCursor();
  116.                 DIBadMount(where, event->message);
  117.             }
  118.             break;
  119.         case mouseDown:
  120.             switch (part = FindWindow(event->where, &wp)) {
  121.                 case inMenuBar:
  122.                     InitCursor();
  123.                     doMenu(MenuSelect(event->where));
  124.                     break;
  125.                 case inSysWindow:
  126.                     SystemClick(event, wp);
  127.                     break;
  128.             }
  129.             break;
  130.         case keyDown:
  131.             if (event->modifiers & cmdKey)
  132.                 doMenu(MenuKey(loword(event->message)));
  133.             break;
  134.     }
  135. }
  136.  
  137.  
  138. /*
  139.  *  doMenu - process menu selection
  140.  *
  141.  */
  142.  
  143. static
  144. doMenu(long choice)
  145. {
  146.     short i = loword(choice);
  147.     Str255 buf;
  148.     WindowPeek wp;
  149.     
  150.     switch (hiword(choice)) {
  151.         case 1:        /*  Apple  */
  152.             GetItem(appleMenu, i, buf);
  153.             OpenDeskAcc(buf);
  154.             break;
  155.         case 2:        /*  File  */
  156.             switch (i) {
  157.                 case 1:        /*  Open  */
  158.                     while (Keys)
  159.                         ;
  160.                     Keys = 4;
  161.                     OpenDeskAcc("\p\0Control Panel");
  162.                     Keys = 0;
  163.                     break;
  164.                 case 2:        /*  Close  */
  165.                     if (wp = (WindowPeek) FrontWindow())
  166.                         CloseDeskAcc(wp->windowKind);
  167.                     break;
  168.                 case 3:        /*  Quit  */
  169.                     ExitToShell();
  170.             }
  171.             break;
  172.         case 3:        /*  Edit  */
  173.             SystemEdit(i - 1);
  174.             break;
  175.     }
  176. }
  177.  
  178.  
  179. static
  180. __A5(void)
  181. {
  182.     asm {
  183.         bsr.s    @1
  184.         dc.l    0
  185. @1        movea.l    (sp)+,a0
  186.     }
  187. }
  188.  
  189.  
  190. static pascal long
  191. myMain(short x0, short x1, short x2, short x3, long x4, long x5, long x6)
  192. {
  193.     extern pascal long _main(short, short, short, short, long, long, long);
  194.  
  195.     __A5();
  196.     asm {
  197.         move.l    (a0),d0
  198.         cmp.l    CurrentA5,d0
  199.         bne.s    @done
  200.         suba.l    a0,a0
  201.         move.l    a5,-(sp)
  202.         movea.l    d0,a5
  203.     }
  204.     x5 = _main(x0, x1, x2, x3, x4, x5, x6);
  205.     asm {
  206.         movea.l    (sp)+,a5
  207.     }
  208. done:
  209.     return(x5);
  210. }
  211.